//
//  FFSettingViewController.swift
//  Clock
//
//  Created by jisa on 2020/5/7.
//  Copyright © 2020 jff. All rights reserved.
//

import UIKit

class FFSettingViewController: UIViewController {
    
    fileprivate var aTableView:UITableView!
    //开关状态
    fileprivate var offonArr:[Bool] = FFStoreHelper.share.FFGetBasicState()
    
    fileprivate var completeBtn:UIButton!
    fileprivate var titleLabel:UILabel!
    
    fileprivate let dic:Dictionary = [
        "基础设置" : ["显示日期", "显示周", "保持常亮"],
        "字体设置" : ["字体样式", "字号"]
    ]

    override var shouldAutorotate: Bool {
        return false
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(true, animated: animated)
    }
        
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        aTableView.frame = view.bounds
        titleLabel.frame = CGRect(x: 0, y: 0, width: view.bounds.size.width, height: 44 * ScaleOf375)
        completeBtn.frame = CGRect(x: view.bounds.size.width - 60 * ScaleOf375, y: 7 * ScaleOf375, width: 60 * ScaleOf375, height: 30 * ScaleOf375)
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
//        let temp:AppDelegate? = UIApplication.shared.delegate as? AppDelegate
//        temp?.isSupportOrientation = false
//        let _ = temp?.application(UIApplication.shared, supportedInterfaceOrientationsFor: view.window)
        FFSetUpTableView()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
//        let temp:AppDelegate? = UIApplication.shared.delegate as? AppDelegate
//        temp?.isSupportOrientation = true
//        let _ = temp?.application(UIApplication.shared, supportedInterfaceOrientationsFor: view.window)
    }

}

extension FFSettingViewController: UITableViewDelegate, UITableViewDataSource {
    
    /// TableView
    fileprivate func FFSetUpTableView() {
        aTableView = UITableView(frame: .zero, style: .grouped)
        aTableView.delegate = self
        aTableView.dataSource = self
        aTableView.tableHeaderView = FFSetHeaderTableView()
        aTableView.rowHeight = 44 * ScaleOf375
        aTableView.register(NSClassFromString("UITableViewCell"), forCellReuseIdentifier: "default")
        view.addSubview(aTableView)
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return dic.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let keys:[String] = dic.keys.sorted()
        let arr:[String] = dic[keys[section]]!
        return arr.count
    }
    
    /// header view
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let vi:UIView = UIView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 44 * ScaleOf375))
        vi.backgroundColor = FFColorRGB(242, 242, 242)
        let label:UILabel = UILabel(frame: CGRect(x: 15, y: 0, width: ScreenWidth - 15, height: 44 * ScaleOf375))
        let keys:[String] = dic.keys.sorted()
        label.text = keys[section]
        label.font = UIFont.systemFont(ofSize: 12 * ScaleOf375, weight: .medium)
        vi.addSubview(label)
        return vi
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 44 * ScaleOf375
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.1
    }
    
    /// cell
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "default")!
        let keys:[String] = dic.keys.sorted()
        let key:String = keys[indexPath.section]
        cell.textLabel?.text = dic[key]?[indexPath.row]
        cell.selectionStyle = .none
        cell.textLabel?.font = UIFont.systemFont(ofSize: 15 * ScaleOf375, weight: .medium)
        if indexPath.section == 0 {
            cell.accessoryView = FFAcquireSwitch(indexPath.row)
        }else {
            cell.accessoryType = .disclosureIndicator
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 1 && indexPath.row == 0 {
            navigationController?.pushViewController(FFFontSettingViewController(), animated: true)
        }else if indexPath.section == 1 && indexPath.row == 1 {
            navigationController?.pushViewController(FFFontSizeViewController(), animated: true)
        }
    }
    
    /// 开关View
    private func FFAcquireSwitch(_ index:Int) -> UISwitch {
        let offOn:UISwitch = UISwitch(frame:  CGRect(x: 0, y: 0, width: 60, height: 30))
        offOn.isOn = offonArr[index]
        offOn.addTarget(self, action: #selector(FFSwitchChange(_:)), for: .touchUpInside)
        offOn.tag = 100 + index
        return offOn
    }
    
    /// 开关状态改变
    @objc private func FFSwitchChange(_ sender:UISwitch) {
        let index = sender.tag - 100
        offonArr[index] = sender.isOn
        if index == 2 {
            UIApplication.shared.isIdleTimerDisabled = sender.isOn
        }
    }
    
    /// 设置TableView 的 headerTableView
    private func FFSetHeaderTableView() -> UIView {
        let vi:UIView = UIView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 44 * ScaleOf375))
        vi.backgroundColor = UIColor.white
        
        titleLabel = UILabel(frame: vi.bounds)
        titleLabel.text = "设置"
        titleLabel.font = UIFont.systemFont(ofSize: 20 * ScaleOf375, weight: .medium)
        titleLabel.textAlignment = .center
        vi.addSubview(titleLabel)
        
        completeBtn = UIButton(type: .custom)
        completeBtn.setTitle("完成", for: .normal)
        completeBtn.titleLabel?.font = UIFont.systemFont(ofSize: 20 * ScaleOf375, weight: .medium)
        completeBtn.setTitleColor(UIColor.black, for: .normal)
        completeBtn.addTarget(self, action: #selector(FFCancelPresent), for: .touchUpInside)
        vi.addSubview(completeBtn)
        
        return vi
    }
    
    /// 取消模态
    @objc private func FFCancelPresent() {
        dismiss(animated: true) {
            FFStoreHelper.share.FFSetUpSelectBasicState(arr:self.offonArr)
            NotificationCenter.default.post(Notification(name: Notification.Name.init(UpdateBasicState)))
        };
    }
}
